<?php

namespace app\admin\controller\oa;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;
use think\exception\ValidateException;

/**
 * 用户自己公告列表
 *
 * @icon fa fa-user
 */
class Relevance extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Relevance;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with([
                    'notice' => function ($query) {
                        $query->withField('id,title');
                    }
                ])
                ->where($where)
                ->where('admin_id',$this->auth->id)
                ->order('id', 'desc')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 改变状态
     *
     */
    public function for(){
        $arr = request()->param();

        $detail = Db::table('fa_oa_relevance')
            ->where('id',$arr['ids'])
            ->find();

        if (empty($detail)){
            $this->error('数据错误');
        }

        if($detail['type'] == 2){
            $update = 1;
        }else{
            $update = Db::table('fa_oa_relevance')
                ->where('id',$arr['ids'])
                ->update([
                    'type' => 2,
                    'update_time' => time(),
                ]);
        }



        if ($update !== false){
            $this->success('成功','',$arr['ids']);
        }else{
            $this->error('状态更新失败');
        }

    }

    /**
     * 改变状态
     *
     */
    public function fors(){
        $arr = request()->param();

        $detail = Db::table('fa_oa_relevance')
            ->where('id',$arr['id'])
            ->find();

        if (empty($detail)){
            $this->error('数据错误');
        }

        if($detail['type'] == 2){
            $update = 1;
        }else{
            $update = Db::table('fa_oa_relevance')
                ->where('id',$arr['id'])
                ->update([
                    'type' => 2,
                    'update_time' => time(),
                ]);
        }



        if ($update !== false){
            $this->success('成功','',$arr['id']);
        }else{
            $this->error('状态更新失败');
        }

    }


    /**
     * 详情
     */
    public function detail()
    {
        $arr = request()->param();
        //$row = $this->model->get(['id' => $arr['id']]);
        $row = Db::table('fa_oa_relevance')
            ->alias('a')
            ->field('a.id,b.title,b.content')
            ->join('fa_oa_notice b','a.notice_id=b.id','left')
            ->where('a.id',$arr['id'])
            ->find();

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }




}
